package com.yu.crm.controller;


import com.yu.crm.domain.pojo.CrmDepartmentMember;
import com.yu.crm.domain.pojo.CrmMenu;
import com.yu.crm.service.CrmMenuService;
import com.yxzz.common.helper.valid.ValidatorHelpers;

import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

import javax.validation.Valid;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author donnie-bin
 * @since 2019-06-10
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/crm-menu")
public class CrmMenuController {

    @Autowired
    CrmMenuService menuService;

    @ApiOperation(value = "新增或修改")
    @PostMapping(value = "/save-or-update")
    public void saveOrUpdate(@RequestBody @Valid CrmMenu menu, BindingResult bindingResult){
        ValidatorHelpers.validateParams(bindingResult);
        menuService.saveOrUpdate(menu);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/remove-by-id/{id}")
    public void removeById(@PathVariable Integer id){
        menuService.removeById(id);
    }

    @ApiOperation(value = "树形列表查询")
    @GetMapping(value = "/tree-list/{pid}")
    public List<CrmMenu> treeList(@PathVariable Integer pid){
        return menuService.treeList(pid);
    }

    @ApiOperation(value = "树形列表查询-带根目录")
    @GetMapping(value = "/tree-list1/{pid}")
    public List<CrmMenu> treeList1(@PathVariable Integer pid){
        return menuService.treeList1(pid);
    }


    /**
     * 通过部门编号向上移动一位排序
     * @param id 部门编号
     */
    @ApiOperation(value = "通过部门编号向上移动一位排序")
    @GetMapping(value = "/update-sort/{id}")
    public void updateSort(@PathVariable Integer id){
        ValidatorHelpers.notEmpty(id, "部门编号不能为空");
        menuService.updateSort(id);
    }

    @ApiOperation(value = "根据当前用户获取菜单列表")
    @GetMapping(value = "/get-user-menus")
    public List<HashMap<String,Object>> getMenusByUserId(){
        return menuService.getMenusByUserId(null);
    }

    @ApiOperation(value = "详情查询")
    @GetMapping(value = "/get-by-id/{id}")
    public CrmMenu getById(@PathVariable Long id){
        ValidatorHelpers.notEmpty(id, "部门编号不能为空");
        return menuService.getById(id);
    }

    @ApiOperation(value = "通过地址url查询地址详情")
    @GetMapping(value = "/get-by-pageRouter")
    public CrmMenu getByPageRouter(@Param("pageRouter") String pageRouter){
        ValidatorHelpers.notEmpty(pageRouter, "地址url不能为空");
        return  menuService.getByPageRouter(pageRouter);
    }
}

